package thread;

import java.util.Scanner;

/**
 * Creared with IntelliJ IDEA.
 * Description:内存可见性问题
 * User:yxd
 * Date:2022-03-23
 * Time:18:32
 */
public class Demo15 {
    //private static int falg = 0;
    private volatile static int falg = 0;//解决内存可见性问题

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            while(falg == 0){//多次进行读操作

            }
            System.out.println("循环结束,t线程结束");
        });
        t.start();
        System.out.println("请输入一个falg的值:");
        Scanner input = new Scanner(System.in);
        falg = input.nextInt();//main线程进行修改值
        //此时我们想达到的结果就是线程推出循环,然后t线程结束
        System.out.println("main线程结束");
    }
}
